<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: fzh<fanzhaohai@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * 店铺商品
 */
class ShopGoodsPromotionModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        array('promotion_price', 'require', '促销价格必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('promotion_price', 'double', '促销价格输入错误', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        array('promotion_stock', 'require', '促销库存必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('promotion_stock', 'number', '促销库存必须是数字', self::VALUE_VALIDATE, '', self::MODEL_BOTH),
        array('promotion_start_time', 'isNowTime', '请选择开始时间', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
        array('promotion_end_time', 'isNowTime', '请选择结束时间', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
//         array('pic_id', 'gtNumber', '请上传图片', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_BOTH),
        array('promotion_start_time', 'strtotime', self::MODEL_BOTH, 'function'),
        array('promotion_end_time', 'strtotime', self::MODEL_BOTH, 'function')
    );
   
    /**
     * @todo 时间不得小于当前时间
     */
    public function isNowTime($promotion_start_time){
        $promotion_start_time = strtotime($promotion_start_time);
        if($promotion_start_time <= 0){
            return false;
        }
        
    }
    
    /**
     * @todo 验证大于0的数字
     */
    public function gtNumber($pic_id){
        $pic_id = (int)$pic_id;
        if($pic_id <= 0){
            return false;
        }
    
    }
   
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     */
    public function getPk(){
        return 'promotion_id';
    }
    
//     /**
//      * @todo 获取促销商品信息
//      * @param $goods_id array()|int 商品id
//      * @return array();
//      */
//     public function getGoodsPromotionShow($goods_id = array()){
//         $goods_id = (array)$goods_id;
//         $data = array();
//         if(empty($goods_id)){
//             return $data;
//         }
        
//         $map['goods_id'] = array('in',$goods_id);
//         $field = 'promotion_id,goods_id,promotion_price,promotion_stock,promotion_start_time,promotion_end_time,pic_id as promotion_pic_id';
//         $list = M('ShopGoodsPromotion')->where($map)->field($field)->order('promotion_id desc')->select();
//         if(!empty($list)){
//             foreach ($list as $key=>$val){
//                 //虚假时间临时用
//                 $start_time = strtotime(date('Y-m-d').' 00:00:00');
//                 $end_time = strtotime(date('Y-m-d').' 23:59:59');
//                 $val['promotion_start_time'] = $val['promotion_start_time'] > $start_time ? $val['promotion_start_time'] : $start_time;
//                 $val['promotion_end_time'] = $val['promotion_end_time'] < $end_time ? $val['promotion_end_time'] : $end_time;
//                 $data[$val['goods_id']] = $val;
//             }
//         }
//         if(count($data) == 1){
//             return $data[0];
//         }
//         return $data;
//     }
    
}